package com.hk.admin.module.system.controller;

import com.hk.admin.common.base.controller.BaseController;
import com.hk.admin.common.constant.GlobalConstants;
import com.hk.admin.common.constant.GlobalEnum;
import com.hk.admin.common.util.CKEditorUtil;
import com.hk.admin.common.util.FileUtil;
import com.hk.admin.module.system.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <br>
 * <b>功能：</b>文件上传<br>
 * <b>作者：</b>Pan.ShiJu<br>
 * <b>日期：</b>2017/4/11 23:27<br>
 */
@RestController
@RequestMapping("/file")
public class FileController extends BaseController {

    @Autowired
    private FileService fileService;

    /**
     * 单图片上传
     *
     * @param file 上传的文件
     * @return 图片的url
     */
    @RequestMapping(value = "/upload/image", method = RequestMethod.POST)
    public String uploadImageSingle(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            throwError(GlobalConstants.FILE_DOES_NOT_EXIST);
        }
        // 判断文件后缀是否正确
        if (!GlobalEnum.IMAGE_FIELDS.containsIgnoreCase(FileUtil.getFileSuffix(file.getOriginalFilename()))) {
            throwError(GlobalConstants.FILE_FORMAT_IS_INCORRECT);
        }
        return fileService.uploadImage(file);
    }

    /**
     * 多图片上传
     *
     * @param request http请求
     * @return 多个图片的url
     */
    @RequestMapping(value = "/upload/multiple/image", method = RequestMethod.POST)
    public List uploadImageMultiple(HttpServletRequest request) {
        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
        List<String> imageUrlList = new ArrayList<>();
        if (files == null) {
            return imageUrlList;
        }
        files.forEach(file -> {
            if (file != null && GlobalEnum.IMAGE_FIELDS.containsIgnoreCase(FileUtil.getFileSuffix(file.getOriginalFilename()))) {
                String imageUrl = fileService.uploadImage(file);
                if (imageUrl != null) imageUrlList.add(imageUrl);
            }
        });
        return imageUrlList;
    }

    /**
     * ckeditor 上传图片处理方法
     * 例如：http://localhost:8080/file/ck/upload/image?CKEditor=editor1&CKEditorFuncNum=1&langCode=zh-cn
     * Post请求中文件名 name="upload"
     *
     * @param upload          文件
     * @param CKEditorFuncNum ckeditor的特有回调标志
     * @param response        http响应
     * @throws IOException
     */
    @RequestMapping(value = "/ck/upload/image", method = RequestMethod.POST)
    public void ckeditorUploadImage(MultipartFile upload, String CKEditorFuncNum, HttpServletResponse response) throws IOException {
        // 设置响应头
        CKEditorUtil.setUpCkeditorResponse(response);
        // 保存文件，返回文件访问路径
        String imageUrl = fileService.uploadImage(upload);
        // 将上传的图片的url返回给ckeditor
        String responseData = CKEditorUtil.getCkeditorResponse(imageUrl, CKEditorFuncNum);
        // 返回响应流
        CKEditorUtil.writerResponse(response, responseData);
    }


}
